package com.ruicar.afs.cloud.afscase.message.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.message.entity.CaseNoticeInfo;
import com.ruicar.afs.cloud.afscase.message.service.CaseNoticeInfoService;
import com.ruicar.afs.cloud.afscase.message.vo.CaseNoticeInfoVO;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ReadEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>description：</p>
 * 案件消息通知处理
 * @author ma_xueyu
 * @date 2020-8-24
 * */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/noticeInfo")
public class CaseNoticeInfoController {

    private final CaseNoticeInfoService caseNoticeInfoService;

    /**
     * 根据用户账号获取案件消息通知
     * @param
     * */
    @GetMapping(value = "/getCaseNoticeInfoByUsername")
    @ApiOperation(value="根据用户账号获取案件未读消息通知")
    public IResponse<List<CaseNoticeInfo>> getCaseNoticeInfoByUsername(){
        List<CaseNoticeInfo> caseNoticeInfoList = caseNoticeInfoService.list(Wrappers.<CaseNoticeInfo>lambdaQuery()
                .eq(CaseNoticeInfo::getIsRead, AfsEnumUtil.key(ReadEnum.UNREAD))
                .eq(CaseNoticeInfo::getReceivedBy, SecurityUtils.getUsername()));
        IPage iPage=new Page();
        iPage.setRecords(caseNoticeInfoList);
        iPage.setTotal(CollectionUtil.isNotEmpty(caseNoticeInfoList)?caseNoticeInfoList.size():0);
        return IResponse.success(iPage);
    }

    /**
     * 根据申请编号获取案件消息通知
     * @param
     * */
    @GetMapping(value = "/getCaseNoticeInfoByApplyNo")
    @ApiOperation(value="根据申请编号获取案件未读消息通知")
    public IResponse<List<CaseNoticeInfo>> getCaseNoticeInfoByApplyNo(@ModelAttribute CaseNoticeInfoVO caseNoticeInfoVO){
        List<CaseNoticeInfo> caseNoticeInfoList = caseNoticeInfoService.list(Wrappers.<CaseNoticeInfo>lambdaQuery()
                .eq(CaseNoticeInfo::getIsRead, AfsEnumUtil.key(ReadEnum.UNREAD))
                .eq(CaseNoticeInfo::getApplyNo,caseNoticeInfoVO.getApplyNo())
                .eq(CaseNoticeInfo::getReceivedBy, SecurityUtils.getUsername()));
        IPage iPage=new Page();
        iPage.setRecords(caseNoticeInfoList);
        iPage.setTotal(CollectionUtil.isNotEmpty(caseNoticeInfoList)?caseNoticeInfoList.size():0);
        return IResponse.success(iPage);
    }

    @PostMapping(value = "/markCaseNoticeInfoAsRead")
    @Transactional
    @ApiOperation(value="标记为已读")
    public IResponse<Boolean> markCaseNoticeInfoAsRead(@RequestBody CaseNoticeInfo caseNoticeInfo){
        caseNoticeInfoService.update(Wrappers.<CaseNoticeInfo>lambdaUpdate()
                .eq(CaseNoticeInfo::getId,caseNoticeInfo.getId())
                .set(CaseNoticeInfo::getIsRead,AfsEnumUtil.key(ReadEnum.READ)));
        return new IResponse<Boolean>().setCode(CommonConstants.SUCCESS);
    }
}
